﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.ComponentModel.Composition;
using Bzure.Common.Core;
using Bzure.Common.Events;
using Bzure.Common.HttpAspects;

namespace Bzure.Security
{
    [Export(typeof(IContractFactory<IHttpAspect>)), ExportMetadata("ProviderName", "_bzureAuthentication")]
    public class SecurityHttpAspectBuilder : IContractFactory<IHttpAspect>
    {
        /// <summary>
        /// 契约实现的版本号
        /// </summary>
        public float ContractVersion { get { return 1.0F; } }
        public IHttpAspect Build()
        {
            return new SecurityHttpAspect();
        }
    }
    public class SecurityHttpAspect : IHttpAspect
    {
        public void InitAspect(IEventComponent<HttpEventOption, EventHandler> factory)
        {
            factory.AddEventHandler(HttpEventOption.BeginRequest, new EventHandler(app_BeginRequest));
        }


        void app_BeginRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Url.AbsolutePath == "/")
                HttpContext.Current.Response.Redirect(string.Format("/{0}", Actor.Public.DefaultArea()));
        }
    }
}
